﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Hangjing.Common;
using Hangjing.SQLServerDAL;
using Hangjing.Model;
using System.Collections.Generic;

public partial class qy_54tss_Admin_User_UserAddMoneyLog : System.Web.UI.Page
{
    private string SqlWhere
    {
        get
        {
            object o = ViewState["SqlWhere"];
            return (o == null) ? "" : Convert.ToString(o);
        }
        set
        {
            ViewState["SqlWhere"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            int delflag = HjNetHelper.GetQueryInt("delflag", 0);
            SqlWhere = "1=1 and delflag ="+ delflag;

            if (delflag > 0)
            {
                lbDelsom.Text = "恢复选定";

            }

            WebUtility.SetDDLCity(DDLArea);

            RightsTool rt = new RightsTool();

            int admincityid = rt.admin.CityID;

            SqlWhere = rt.getAdminFeeWhere(SqlWhere);

            IList<AccountsInfo> Accountslist = new Accounts().GetsubList();
            IList<DeliverGroupInfo> DeliverGrouplist = SectionProxyData.GetEdelivergroupList();
            IList<SectionInfo> Sectionlist = SectionProxyData.GetSectionList();

            if (admincityid > 0)
            {
                DeliverGrouplist = SectionProxyData.GetEdelivergroupList().Where(a=>a.Status == admincityid).ToList();
                Sectionlist = SectionProxyData.GetSectionList().Where(a => a.cityid == admincityid).ToList();
            }

            IList<AccountsInfo> sortaccountlist = new List<AccountsInfo>();
            IList<AccountsInfo> firstaccounts = Accountslist.Where(a => a.Parentid == 0).ToList();
            foreach (var item in firstaccounts)
            {
                sortaccountlist.Add(item);
                foreach (var sub in  Accountslist.Where(a => a.Parentid == item.ID).ToList())
                {
                    sortaccountlist.Add(sub);
                }
            }

            foreach (var info in sortaccountlist)
            {
                info.tempname = info.classname;
                if (info.Parentid > 0)
                {
                    info.tempname = HttpUtility.HtmlDecode("&nbsp;&nbsp;&nbsp;&nbsp;" + info.tempname);
                }
            }

            WebUtility.BindList("id", "tempname", sortaccountlist, ddlcount);
            WebUtility.BindList("id", "classname", DeliverGrouplist, ddlDeliverGroup);
            WebUtility.BindList("sectionid", "sectionname", Sectionlist, ddlState);



        }
    }

    AccountingDetail dal = new AccountingDetail();

    protected void BindData()
    {
        this.AspNetPager1.RecordCount = dal.GetCount(SqlWhere);
        IList<AccountsInfo> accountlist = new Accounts().GetsubList();
        IList<DeliverGroupInfo> DeliverGrouplist = SectionProxyData.GetEdelivergroupList();
        IList<SectionInfo> Sectionlist = SectionProxyData.GetSectionList();
        IList<AccountingDetailInfo> datalist = dal.GetList(AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, SqlWhere, "dataid", 1);
        foreach (var item in datalist)
        {
            foreach (var account in accountlist)
            {
                if (item.UserId == account.ID)
                {
                    item.accountname = account.classname;
                    break;
                }
                
            }
            foreach (var account in accountlist)
            {

                if (item.PayDate == account.ID)
                {
                    item.subaccountname = account.classname;
                    break;
                }

            }
            foreach (var account in Sectionlist)
            {

                if (item.State == account.SectionID)
                {
                    item.accountuser = account.SectionName;
                    break;
                }

            }
            foreach (var group in DeliverGrouplist)
            {

                if (item.PayType == group.ID)
                {
                    item.delivername +="-"+ group.classname;
                    break;
                }

            }
        }

        this.rtpGifts.DataSource = datalist;
        this.rtpGifts.DataBind();
        //帐单

        IList<AccountsInfo> list = new Accounts().GetsubList().Where(a=>a.Parentid == 0).ToList();

        decimal allmoney = 0;

        IList<AccountingDetailInfo> sums = dal.GetBills(SqlWhere,"userid");
        foreach (var item in sums)
        {
            allmoney += item.AddMoney;
        }
        foreach (var item in list)
        {
            foreach (var sum in sums)
            {
                if (item.ID ==sum.UserId)
                {
                    item.pic = sum.AddMoney.ToString();
                }
            }
        }

        lbIncome.InnerText = allmoney.ToString();
        WebUtility.BindRepeater(rptppt, list);

    }

    protected void rtpGifts_ItemCommand(object sender, RepeaterCommandEventArgs e)
    {
        string type = e.CommandName;
        switch (type)
        {
            case "del":
                if (dal.DelAccountingDetail(Convert.ToInt32(e.CommandArgument)) > 0)
                {
                    AlertScript.RegScript(this.Page,  "tipsWindown('提示信息','text:删除成功!','250','150','true','1000','true','text');init();");

                    BindData();
                }
                else
                {
                    AlertScript.RegScript(this.Page,  "tipsWindown('提示信息','text:删除失败!','250','150','true','1000','true','text');init();");
                }
                break;
        }
    }

    protected void DelList_Click(object sender, EventArgs e)
    {
        int delflag = HjNetHelper.GetQueryInt("delflag", 0) ^ 1 ;

        if (dal.DelOrRecoverRecord(this.hidDels.Value,UserHelp.GetAdmin().AdminName, delflag) > 0)
        {
            AlertScript.RegScript(this.Page, "tipsWindown('提示信息','text:删除成功!','250','150','true','1000','true','text');init();");
            BindData();
        }
        else
        {
            AlertScript.RegScript(this.Page,  "tipsWindown('提示信息','text:删除失败!','250','150','true','1000','true','text');init();");
        }
    }

    protected void btSearch_Click(object sender, EventArgs e)
    {
        InitSqlwhere();
        BindData();
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BindData();
    }

    /// <summary>
    /// 时间变化搜索
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void settime_Click(object sender, EventArgs e)
    {
        string starttime = this.tbStartTime.Text;
        string endtime = this.tbEndTime.Text;
        Button bt = (Button)sender;
        switch (bt.ID)
        {
            case "btpre"://前一天
                if (starttime != "")
                {
                    tbStartTime.Text = Convert.ToDateTime(starttime).AddDays(-1).ToShortDateString() + "";
                }
                else
                {
                    return;
                }
                if (endtime != "")
                {
                    tbEndTime.Text = Convert.ToDateTime(endtime).AddDays(-1).ToShortDateString() + "";
                }
                else
                {
                    return;
                }
                break;
            case "btyestoday"://昨天
                tbStartTime.Text = DateTime.Now.AddDays(-1).ToShortDateString() + "";
                tbEndTime.Text = DateTime.Now.ToShortDateString() + "";

                break;
            case "tbtoday"://今天
                tbStartTime.Text = DateTime.Now.ToShortDateString() + "";
                tbEndTime.Text = DateTime.Now.AddDays(1).ToShortDateString() + "";

                break;
            case "btnext"://后一天
                if (starttime != "")
                {
                    tbStartTime.Text = Convert.ToDateTime(starttime).AddDays(1).ToShortDateString() + "";
                }
                else
                {
                    return;
                }
                if (endtime != "")
                {
                    tbEndTime.Text = Convert.ToDateTime(endtime).AddDays(1).ToShortDateString() + "";
                }
                else
                {
                    return;
                }
                break;
            case "btweek"://本周
                {
                    DateTime weekfirst = TimeHelper.WeekFirst();//本周一
                    DateTime StartTime = DateTime.Now;
                    DateTime EndTime = DateTime.Now;
                    if (TimeHelper.dayOfWeek() == 1) //现在已经是下一周了
                    {
                        if (DateTime.Now.Hour >= 4)
                        {
                            StartTime = weekfirst;
                            EndTime = weekfirst.AddDays(7);
                        }
                        else
                        {
                            StartTime = weekfirst.AddDays(-7);
                            EndTime = weekfirst;
                        }

                    }
                    else
                    {
                        StartTime = weekfirst;
                        EndTime = weekfirst.AddDays(7);
                    }

                    tbStartTime.Text = StartTime.ToString("yyyy-MM-dd");
                    tbEndTime.Text = EndTime.ToString("yyyy-MM-dd"); ;
                }

                break;
            case "btpreweek"://上一周
                {
                    if (starttime != "")
                    {
                        tbStartTime.Text = Convert.ToDateTime(starttime).AddDays(-7).ToShortDateString() + "";
                    }
                    else
                    {
                        return;
                    }
                    if (endtime != "")
                    {
                        tbEndTime.Text = Convert.ToDateTime(endtime).AddDays(-7).ToShortDateString() + "";
                    }
                    else
                    {
                        return;
                    }

                }
                break;
            case "btnextweek"://后一周
                if (starttime != "")
                {
                    tbStartTime.Text = Convert.ToDateTime(starttime).AddDays(7).ToShortDateString() + "";
                }
                else
                {
                    return;
                }
                if (endtime != "")
                {
                    tbEndTime.Text = Convert.ToDateTime(endtime).AddDays(7).ToShortDateString() + "";
                }
                else
                {
                    return;
                }
                break;

            case "btmounth"://本月
                {
                    DateTime mounthfirst = TimeHelper.MounthFirst();//本月一号
                    DateTime StartTime = DateTime.Now;
                    DateTime EndTime = DateTime.Now;
                    if (DateTime.Now.Day == 1) //现在已经是下一月了
                    {
                        if (DateTime.Now.Hour >= 4)
                        {
                            StartTime = mounthfirst;
                            EndTime = mounthfirst.AddMonths(1);
                        }
                        else
                        {
                            StartTime = mounthfirst.AddMonths(-1);
                            EndTime = mounthfirst;
                        }

                    }
                    else
                    {
                        StartTime = mounthfirst;
                        EndTime = mounthfirst.AddMonths(1);
                    }

                    tbStartTime.Text = StartTime.ToString("yyyy-MM-dd");
                    tbEndTime.Text = EndTime.ToString("yyyy-MM-dd"); ;
                }

                break;
            case "btpremounth"://前一月
                {
                    if (starttime != "")
                    {
                        tbStartTime.Text = Convert.ToDateTime(starttime).AddMonths(-1).ToShortDateString() + "";
                    }
                    else
                    {
                        return;
                    }
                    if (endtime != "")
                    {
                        tbEndTime.Text = Convert.ToDateTime(endtime).AddMonths(-1).ToShortDateString() + "";
                    }
                    else
                    {
                        return;
                    }

                }
                break;
            case "btnextmounth"://后一月
                if (starttime != "")
                {
                    tbStartTime.Text = Convert.ToDateTime(starttime).AddMonths(1).ToShortDateString() + "";
                }
                else
                {
                    return;
                }
                if (endtime != "")
                {
                    tbEndTime.Text = Convert.ToDateTime(endtime).AddMonths(1).ToShortDateString() + "";
                }
                else
                {
                    return;
                }
                break;
            default:
                break;
        }
        InitSqlwhere();

        BindData();
    }


    /// <summary>
    /// 生成查询条件
    /// </summary>
    protected void InitSqlwhere()
    {
        SqlWhere = "1=1 and delflag =" + HjNetHelper.GetQueryInt("delflag", 0);
        RightsTool rt = new RightsTool();
        SqlWhere = rt.getAdminFeeWhere(SqlWhere);
        if (tbKeyword.Text.Trim() != "")
        {
            SqlWhere += " and  Inve2 like '%" + WebUtility.InputText(tbKeyword.Text.Trim()) + "%'";
        }
        if (this.tbStartTime.Text != "")
        {
            SqlWhere += " and AddDate > '" + WebUtility.InputText(tbStartTime.Text) + "'";
        }
        if (this.tbEndTime.Text != "")
        {
            SqlWhere += " and AddDate < '" + WebUtility.InputText(tbEndTime.Text) + " 23:59:59'";
        }

        if (this.tbaddstart.Text != "")
        {
            SqlWhere += " and systime > '" + WebUtility.InputText(tbaddstart.Text) + "'";
        }
        if (this.tbaddend.Text != "")
        {
            SqlWhere += " and systime < '" + WebUtility.InputText(tbaddend.Text) + " 23:59:59'";
        }

        if (this.ddlcount.SelectedValue != "-1")
        {
            SqlWhere += " and ( UserId = " + this.ddlcount.SelectedValue + " or PayDate = "+ddlcount.SelectedValue+" )";
        }
        if (DDLArea.SelectedValue != "0")
        {
            SqlWhere += " and paystate = " + DDLArea.SelectedValue + "";
        }
        if (ddlDeliverGroup.SelectedValue != "0")
        {
            SqlWhere += " and PayType = " + ddlDeliverGroup.SelectedValue + "";
        }
        if (ddlState.SelectedValue != "-1")
        {
            SqlWhere += " and State = " + ddlState.SelectedValue + "";
        }
    }


    protected void btExport_Click(object sender, EventArgs e)
    {
        //判断权限
        int _rs = WebUtility.checkOperator_super(7);
        if (_rs == 0)
        {
            AlertScript.RegScript(this.Page,  "alert('无操作权限','success','true',5);init();");
            return;
        }

        WebUtility.FixsetCookie("webStatisticsExcleOrder", Server.UrlEncode(SqlWhere), 1);
        Response.Redirect("UserAddMoneyToExcel.aspx");
    }
}
